package com.leyou.cart.controller;

import com.leyou.cart.pojo.Cart;
import com.leyou.cart.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by 冯金平 on 2023/3/23 11:25
 */
@Controller
@RequestMapping("/")
public class CartController {
    @Autowired
    private CartService cartService;
//根据请求方法自动命中,这两个方法
    /**
     * 添加购物车
     *
     * @param cart 车
     * @return {@link ResponseEntity}<{@link Void}>
     */
    @PostMapping
    public ResponseEntity<Void> addCart(@RequestBody Cart cart) {
        this.cartService.addCart(cart);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 查询购物车列表
     *
     * @return {@link ResponseEntity}<{@link List}<{@link Cart}>>
     */
    @GetMapping
    public ResponseEntity<List<Cart>> queryCartList(){
        List<Cart> list = this.cartService.queryCartList();
        if (CollectionUtils.isEmpty(list)){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
        return ResponseEntity.ok(list);
    }

    /**
     * 更新商品数量
     *
     * @param cart 车
     * @return {@link ResponseEntity}<{@link Void}>
     */
    @PutMapping
    public ResponseEntity<Void> updateNum(@RequestBody Cart cart){
        this.cartService.updateNum(cart);
        return ResponseEntity.noContent().build();
        //无内容响应但是操作成功
    }

    /**
     * 删除商品
     *
     * @param skuId sku id
     * @return {@link ResponseEntity}<{@link Void}>
     */
    @DeleteMapping("{skuId}")
    public ResponseEntity<Void> deleteShop(@PathVariable("skuId") String skuId){
        this.cartService.deleteShop(skuId);
        return ResponseEntity.ok().build();
    }
}
